﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Presenters
{
    /// <summary>
    /// Presenter for the database list upgrade window
    /// </summary>
    public class DBListUpgradePresenter : PresenterBase<Views.IDBListUpgradeView>
    {
        /// <summary>
        /// Initialise the Database List Upgrade presenter
        /// </summary>
        /// <param name="view">An instance the implements IDBListUpgradeView</param>
        /// <param name="dbListToUpgrade">The list of databases that we will be upgrading</param>
        public DBListUpgradePresenter(Views.IDBListUpgradeView view, List<Models.Database> dbListToUpgrade)
            : base(view)
        {
            m_DbListToUpgrade = dbListToUpgrade;

            // Populate the list of avaialble server plug ins
            View.AvailableServerPlugins = Services.ServersService.GetServerPluginList();
        }

        /// <summary>
        /// Upgrade the list of databases so that those without a valid server id are given the server id from the
        /// selected plug in
        /// </summary>
        public void UpgradeDBList()
        {
            Guid serverID = View.SelectedServerPluginID;

            if (serverID != Guid.Empty)
            {
                // We have a valid server ID so loop through the list of databases to upgrade and, for any that have
                // no server ID, assign them the selected server ID
                foreach (Models.Database db in m_DbListToUpgrade)
                {
                    if (db.DatabaseServer.ServerID == Guid.Empty)
                    {
                        db.DatabaseServer.ServerID = serverID;
                    }
                }
            }
        }

        /// <summary>
        /// The list of databases that we will upgrade
        /// </summary>
        private List<Models.Database> m_DbListToUpgrade;
    }
}
